package o2o.web.actions.system;


import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYPinyinHelper;
import com.jinyou.utils.common.SysCommon;
import com.jinyou.utils.common.ValidateUtil;
import o2o.area.ChnAreaMemory;
import o2o.area.CityInfo;
import o2o.area.CountyInfo;
import o2o.area.ProvinceInfo;
import o2o.cache.area.CityMemory;
import o2o.cache.area.CountyMemory;
import o2o.cache.area.ProvinceMemory;

import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.sys.SysCityAdmin;
import o2o.dao.PO.user.UserInfo;
import o2o.system.data.SYS_SETTINGS;
import o2o.web.actions.company.SysCityCompanyMan;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

//省市区等
@Controller
public class SysAreaAction {

    //获取所有的省市区
    @RequestMapping(method = RequestMethod.POST, value = "/area/list")
    @ResponseBody
    public String sysAreaList(String token) {
        List<ProvinceInfo> list = ChnAreaMemory.getInstance().getAll();
        if (list == null || list.size() < 1) {
            return AjaxResult.successResult(0, null);
        }
        Collections.sort(list, Comparator.comparingInt(ProvinceInfo::getOrderNo).reversed());
        return AjaxResult.successResult(list.size(), list);
    }


    /**
     * 获取代理的省市区县
     *
     * @param cityAdmin
     * @return
     */
    public List<ProvinceInfo> listOfProvinceByAdmin(SysCityAdmin cityAdmin) {
        String province = cityAdmin.getProvince();
        String city = cityAdmin.getCity();
        String county = cityAdmin.getCounty();
//        if (ValidateUtil.isNotNull(province) && province.lastIndexOf("省") > 0) {
//            province = province.substring(0, province.lastIndexOf("省"));
//        }
//        province = province == null ? "" : province;
//        if (ValidateUtil.isNotNull(city) && city.lastIndexOf("市") > 0) {
//            city = province.substring(0, city.lastIndexOf("市"));
//        }

        // 省
        if (cityAdmin.getLevel() != null && cityAdmin.getLevel() == 4) {
            List<ProvinceInfo> list = ChnAreaMemory.getInstance().getAll();
            if (ValidateUtil.isAbsList(list) && ValidateUtil.isNotNull(province)) {
                list = list.stream().filter(item -> {
                    if (!SysCommon.hasKeyword(item.getProvince(), province)) {
                        return false;
                    }
                    return true;
                }).collect(Collectors.toList());
                return list;
            }
            return ValidateUtil.isAbsList(list) ? list : Collections.emptyList();
        } else if (cityAdmin.getLevel() != null && cityAdmin.getLevel() == 3) {
            // 市代
            if (ValidateUtil.isNull(city)) {
                return Collections.emptyList();
            }
            List<CityInfo> cityInfoList = CityMemory.getInstance().list(city, null);
            if (!ValidateUtil.isAbsList(cityInfoList)) {
                return Collections.emptyList();
            }
            List<CityInfo> cityInfos = new ArrayList<>();
            cityInfos.add(cityInfoList.get(0));
            // 区域
            List<CountyInfo> countyInfos = CountyMemory.getInstance().list(null, cityInfoList.get(0).getCityCode());
            if (ValidateUtil.isAbsList(cityInfoList)) {
                cityInfos.get(0).setCounties(countyInfos);
            }
            // 省
            List<ProvinceInfo> provinceList = ProvinceMemory.getInstance().list(null, cityInfos.get(0).getProvCode());
            if (!ValidateUtil.isAbsList(provinceList)) {
                return Collections.emptyList();
            }
            List<ProvinceInfo> provinceInfos = new ArrayList<>();
            provinceInfos.add(provinceList.get(0));
            provinceInfos.get(0).setCities(cityInfos);
            return provinceInfos;
        } else if (cityAdmin.getLevel() != null && cityAdmin.getLevel() == 2) {
            // 区县代理
            if (ValidateUtil.isNull(county)) {
                return Collections.emptyList();
            }
            List<CountyInfo> countyInfoList = CountyMemory.getInstance().list(county, null);
            if (!ValidateUtil.isAbsList(countyInfoList)) {
                return Collections.emptyList();
            }
            List<CountyInfo> countyInfos = new ArrayList<>();
            countyInfos.add(countyInfoList.get(0));
            // 获取城市
            List<CityInfo> cityInfoList = CityMemory.getInstance().getByCityCode(countyInfos.get(0).getCityCode());
            if (!ValidateUtil.isAbsList(cityInfoList)) {
                return Collections.emptyList();
            }
            List<CityInfo> cityInfos = new ArrayList<>();
            cityInfos.add(cityInfoList.get(0));
            cityInfos.get(0).setCounties(countyInfoList);
            // 获取省
            List<ProvinceInfo> provinceList = ProvinceMemory.getInstance().list(null, cityInfos.get(0).getProvCode());
            if (!ValidateUtil.isAbsList(provinceList)) {
                return Collections.emptyList();
            }
            List<ProvinceInfo> provinceInfos = new ArrayList<>();
            provinceInfos.add(provinceList.get(0));
            provinceInfos.get(0).setCities(cityInfos);
            return provinceInfos;
        }
        return Collections.emptyList();
    }


    // 获取省市区的汉语拼音
    @RequestMapping(method = RequestMethod.POST, value = "/admin/area/city2han")
    @ResponseBody
    public String getCityHan(String province, String city, String county) {
        if (null != SYS_SETTINGS.HAS_MULTILINGUAL && 0 == 1 - SYS_SETTINGS.HAS_MULTILINGUAL) {
            if (ValidateUtil.isNotNull(province)) {
                province = JYPinyinHelper.isChinese(province) ? JYPinyinHelper.toHanyuPinyin(province) : province.toLowerCase();
            }
            if (ValidateUtil.isNotNull(city)) {
                city = JYPinyinHelper.isChinese(city) ? JYPinyinHelper.toHanyuPinyin(city) : city.toLowerCase();
            }
            if (ValidateUtil.isNotNull(county)) {
                county = JYPinyinHelper.isChinese(county) ? JYPinyinHelper.toHanyuPinyin(county) : county.toLowerCase();
            }
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("province", province);
        map.put("city", city);
        map.put("county", county);

        return AjaxResult.successResult(map);
    }
}
